# Create your views here.
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.response import Response
from rest_framework.views import APIView
import logging
logger = logging.getLogger('django')
import datetime
from activity.active_serializers import ActiveListSerializer, ActiveDetialSerializer
from activity.utils import MyPage

from users.models import Gathering

# 返回活动列表
class ActiveView(ListAPIView):
    queryset = Gathering.objects.all().order_by('id')
    serializer_class = ActiveListSerializer

    pagination_class = MyPage

# 返回活动详细页面
class ActiveDetialView(RetrieveAPIView):
    queryset = Gathering.objects.all()
    serializer_class = ActiveDetialSerializer

# 报名活动
class UserJoinActive(APIView):

    def post(self, request, pk):
        # 活动对象
        active = Gathering.objects.get(id=pk)
        print(active.users)
        if not active:
            raise ValueError('缺少参数')

        try:
            if request.user in active.users.all():
                active.users.remove(request.user)
                return Response({'success': True,
                                 'message': '取消报名成功'})
            else:
                active.users.add(request.user)
                return Response({'success': True,
                                 'message': '报名成功'})
        except Exception as e:
            return Response({'code': 400,
                             'errmsg': '数据错误'})













